package com.hardydou.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hardydou.common.constant.Const;
import com.hardydou.system.mapper.SystemJobMapper;
import com.hardydou.system.mapper.SystemResMapper;
import com.hardydou.system.mapper.SystemRoleMapper;
import com.hardydou.system.mapper.SystemUserMapper;
import com.hardydou.system.model.LoginUserDetails;
import com.hardydou.system.model.SystemRes;
import com.hardydou.system.model.SystemRole;
import com.hardydou.system.model.SystemUser;
import com.hardydou.system.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.sql.Struct;
import java.util.*;

/**
 * @author ：hardy
 * @description：SystemUserService.java
 * @date ：Created in 2020-03-04
 * @Copyright ：Hardydou.com
 */
@Service
public class SystemUserServiceImpl implements ISystemUserService, UserDetailsService {

    @Autowired
    private SystemJobMapper jobMapper;

    @Autowired
    private SystemUserMapper userMapper;


    @Override
    public SystemUser update(SystemUser user) {
        userMapper.updateByPrimaryKeySelective(user);
        String jobIdsStr = user.getJobIds();
        jobMapper.unBindFromUser(user.getUserId());
        long[] jobIds = StrUtil.splitToLong(jobIdsStr, ",");
        if (jobIds.length > 0) {
            jobMapper.bindToUser(user.getUserId(), jobIds);
        }
        return user;
    }

    @Override
    public List<SystemUser> list(Map<String, Object> params) {
        return userMapper.findAll(params);
    }

    @Override
    public SystemUser findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public SystemUser findByUserId(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void deleteByUserId(long userId) {
        //标记删除数据
        SystemUser user = new SystemUser();
        user.setUserId(userId);
        user.setStatus(Const.UserStatus.LOCKED.val);
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public SystemUser addUser(SystemUser user) {
        Date date = new Date();
        user.setCtime(date);
        user.setUtime(date);
        //设置密码
        PasswordEncoder pe = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        user.setPassword(pe.encode(user.getPassword()));
        //状态
        userMapper.insertSelective(user);
        //岗位信息
        String jobIdsStr = user.getJobIds();
        long[] jobIds = StrUtil.splitToLong(jobIdsStr, ",");
        System.out.println(jobMapper);
        if (jobIds.length > 0) {
            jobMapper.bindToUser(user.getUserId(), jobIds);
        }
        return user;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查数据库
        SystemUser user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("账户不存在");
        }
        return new LoginUserDetails(user);
    }

}
